﻿using Abp.Dependency;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using ARchGL.Platform.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using RestSharp;

namespace ARchGL.Platform.BackgroundWorker
{
    /// <summary>
    /// 保持服务持续运行，不被自动回收
    /// </summary>
    public class KeepLiveWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IConfigurationRoot configuration;

        public KeepLiveWorker(AbpTimer timer, IHostingEnvironment env) : base(timer)
        {
            configuration = env.GetAppConfiguration();
            Timer.Period = 600000; //10分钟一次轮询
            //Timer.Period = 10000;
        }

        protected override void DoWork()
        {
            Knock();
        }

        /// <summary>保留请求信息</summary>
        public void Knock()
        {
            var request = new RestRequest("/api/TokenAuth/Knock", Method.GET);
            var client = new RestClient(configuration["App:ServerRootAddress"]);
            var response = client.Execute(request);
        }
    }
}
